/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package application.controller;

import application.model.EntityAccomplishment;
import application.model.EntityLookUp;
import application.service.BlacklistService;
import application.service.EntityAccomplishmentService;
import application.service.EntitySearchResultsService;
import application.validator.SearchFormValidator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes
public class EntityDetailsController {

    @Autowired
    private EntityAccomplishmentService entityAccomplishmentService;
    @Autowired
    private EntitySearchResultsService entitySearchResultsService;
    @Autowired
    private BlacklistService blacklistService;    
    
    //public ModelAndView showSearch(@PathVariable String id, BindingResult result) {
    @RequestMapping(value = "/entityDetails", method = RequestMethod.GET, params = "eid")
    public ModelAndView showSearch(@RequestParam("eid") String entityID, @RequestParam("kwds") String keywords) {
    
        try {
            SearchFormValidator sfv = new SearchFormValidator(); //input validation tool
            HashMap<String, String> validatedFormValues = new HashMap<String, String>(); //contains validated input values

            validatedFormValues.put("searchEntityID", sfv.validateSearchTerm(entityID));

            List searchResults = entitySearchResultsService.listEntityLookUpByID(validatedFormValues.get("searchEntityID"));

            //if one result only
            EntityLookUp esr = (EntityLookUp) searchResults.get(0);
            //String entityName = esr.getEntityFname() + " " + esr.getEntityMname() + " " + esr.getEntitySname();
            List<EntityAccomplishment> listOfEntityAccomplishments = entityAccomplishmentService.listSelectedEntityAccomp(validatedFormValues.get("searchEntityID"));

            return new ModelAndView("entityDetails")
                    .addObject("visualDataSource","''")
                    .addObject("entitydetails", esr)
                    .addObject("accomplishmentresults", listOfEntityAccomplishments)
                    .addObject("entityid", entityID)
                    .addObject("keywordsForEntityDetailsPage",keywords)
                    .addObject("highestWeightValue", 0.0);
                    //addObject("bodyTagOnload","onload = \"highlightSearchTerms('"+keywords+"')\"");
        } catch (Exception e) {
            return new ModelAndView("entityDetails").addObject("errorMsg", null).addObject("entityid", entityID);
        }
    }

    @RequestMapping(value = "/entityDetails", method = RequestMethod.GET)
    public ModelAndView showSearchDefault() {

            return new ModelAndView("entityDetails").addObject("errorMsg", "Wrong parameters!");

    }
    
    //get keywords
    private ArrayList<String> getKeywords(String kwrds) {
        ArrayList<String> listOfKwrds = new ArrayList(Arrays.asList(kwrds.split(";;")));

        return listOfKwrds;
    }
}
